package edu.ucla.util.code;

import java.util.*;
import java.util.regex.*;
import java.io.*;

/** @author Keith Cascio
	@since 032405 */
public abstract class AbstractScriptGenius implements ScriptGenius
{
	/*public static final String STR_FILENAME_COMPILE = "cbcompile";
	public static final String STR_FILENAME_RUN = "cbrun";

	public void writeAllScripts( File directory ) throws Exception{
		if( (directory == null) || (!directory.exists()) || (!directory.isDirectory()) ) throw new IllegalArgumentException();
		String pathPre = directory.getAbsolutePath() + File.separator;
		writeCompileScript( new File( pathPre + STR_FILENAME_COMPILE + "." + getScriptFileExtension() ) );
		writeRunScript( new File( pathPre + STR_FILENAME_RUN + "." + getScriptFileExtension() ) );
	}

	public void writeCompileScript( File ofile ) throws Exception{
		myDescriptionComment = "This script compiles Java code generated by Code Bandit";
		myInputFileDescription = "<*.java files>";
		myUsageExample = "FooTutorial.java";
		String command = getCompileCommand();
		writeScript( ofile, myDescriptionComment, myInputFileDescription, myUsageExample, command );
	}

	public void writeRunScript( File ofile ) throws Exception{
		myDescriptionComment = "This script runs Java classes generated by Code Bandit";
		myInputFileDescription = "<*.class files>";
		myUsageExample = "FooTutorial";
		String command = getRunCommand();
		writeScript( ofile, myDescriptionComment, myInputFileDescription, myUsageExample, command );
	}

	private void writeScript( File ofile, String description, String fileextension, String usageexample, String command ) throws Exception{
		myDescriptionComment = description;
		myInputFileDescription = fileextension;
		myUsageExample = usageexample;
		PrintStream out = printStream( ofile );
		writeHeader( out );
		out.println( getCommentToken() + " " + myDescriptionComment );
		out.println();
		writeArgValidation( out );
		writeCommandPrep( out, command );
		out.println( command );
		writeCommandPost( out, command );
		writeTail( out );
		out.close();
	}

	public String getDescriptionComment(){
		return myDescriptionComment;
	}

	public String getUsage(){
		return myInputFileDescription;
	}

	public String getUsageExample(){
		return myUsageExample;
	}

	private String myDescriptionComment, myInputFileDescription, myUsageExample;*/

	public void writeArgValidation( Script script, PrintStream out ){}

	public void writeTail( Script script, PrintStream out ){}

	public void writeCommandPrep( PrintStream out, String command ){
		out.println();
	}

	public void writeCommandPost( PrintStream out, String command ){
		out.println();
	}

	private static PrintStream printStream( File ofile ) throws Exception{
		return new PrintStream( new FileOutputStream( ofile ) );
	}

	public void writeHeader( PrintStream out ){
		out.println( getCommentToken() + " Keith Cascio " + ModelCoder.makeDate() );
		out.println();
	}

	public String quote( String command ){
		return "\"" + command + "\"";
	}

	public String escapeQuotes( String str ){
		return str.replaceAll( "\\Q\"\\E", "\\\\\"" );
	}

	public String formSystemCall( String[] cmdarray ){
		String ret = "";

		if( (cmdarray == null) || (cmdarray.length < 1) ) return ret;
		for( int i=0; i<cmdarray.length; i++ ){
			ret += quote( cmdarray[i] ) + " ";
		}
		return ret;
	}

	/*public String getCompileCommand() throws Exception{
		if( myPathJavac == null ){
			myPathJavac = getJavaExecutablePath( "javac" );
			if( myPathJavac == null ) throw new Exception( "javac not found" );
		}
		if( myClasspathInflib == null ){
			myClasspathInflib = getInflibClasspath();
			if( myClasspathInflib == null ) throw new Exception( "inflib not found" );
		}
		return quoteCommand( myPathJavac.getAbsolutePath() ) +
				" -classpath " +
				quoteCommand( myClasspathInflib.getAbsolutePath() ) +
				" " +
				getWildArgumentToken();
	}

	public String getRunCommand() throws Exception{
		if( myPathJava == null ){
			myPathJava = getJavaExecutablePath( "java" );
			if( myPathJava == null ) throw new Exception( "java not found" );
		}
		if( myClasspathInflib == null ){
			myClasspathInflib = getInflibClasspath();
			if( myClasspathInflib == null ) throw new Exception( "inflib not found" );
		}
		return quoteCommand( myPathJava.getAbsolutePath() ) +
				" -classpath " +
				quoteCommand( "." + File.pathSeparator + myClasspathInflib.getAbsolutePath() ) +
				" " +
				getWildArgumentToken();
	}*/
}
